<?php

require_once "dbconnect.php";

//mandatory fields
$username = $_POST['username'];
if( empty($username) ) $username = $_GET['username'];

$password = $_POST['password'];
if( empty($password) ) $password = $_GET['password'];

$email = $_POST['email'];
if( empty($email) ) $email = $_GET['email'];

$firstname = $_POST['firstname'];
if( empty($firstname) ) $firstname = $_GET['firstname'];

$lastname = $_POST['lastname'];
if( empty($lastname) ) $lastname = $_GET['lastname'];

//optional fields
$country = $_POST['country'];
if( empty($country) ) $country = $_GET['country'];

$telephone = $_POST['telephone'];
if( empty($telephone) ) $telephone = $_GET['telephone'];

$subscribe = $_POST['subscribe'];
if( empty($subscribe) ) $subscribe = $_GET['subscribe'];

$city = $_POST['city'];
if( empty($city) ) $city = $_GET['city'];

$address1 = $_POST['address1'];
if( empty($address1) ) $address1 = $_GET['address1'];
$dateinsert = date("Y-m-d H:i:s");
//echo $dateinsert;
$error = array("status"=>"error");
$error2 = array("status"=>"error");

$sql1 = "SELECT username FROM user WHERE username='".$username."'";
if( $db->query($sql1)->num_rows > 0 ) {
	//echo "User existed<br>";
	echo json_encode($error);
	exit();
}

$sql = "INSERT INTO user VALUES('','1','$username','".md5($password)."','$firstname','$lastname','$email','','','1','".$dateinsert."')";
if( !($res = $db->query($sql)) ) $return = $error;
if( $db->countAffected() > 0 ){
	$return = array("status"=>"ok", "user"=>array("username"=>$username));
	//TODO: send confirmation email
	//send_email($email);
} else {
	$return = array("status"=>"error");
}
echo json_encode($return);

?>
